﻿using EventMgrLib;
using SmartMotion.Core;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace SmartMotion.AllPages.DockPanels
{
    public partial class FrmCreateTool : UIForm
    {
     
        private  ActionItem _customTool = new ActionItem();
        //参数类型
        public List<Type> TypeList { get; set; }

        public List<MethodInfo> FunList { get; set; } = new List<MethodInfo>();

        public List<string> ParaTypes { get; set; } = new List<string>();
        //参数名称
        public List<string> ParaNames { get; set; } = new List<string>();

        public FrmCreateTool()
        {
            InitializeComponent();
        }

        private void FrmCreateTool_Load(object sender, EventArgs e)
        {
            RefreshDllAndIcon(false);

            var groups = ToolMgr.Ins.Tools.Select(tool => tool.Group).ToList().Distinct().ToList();
            groups.ForEach(group => cbGroup.Items.Add(group));

        }

        private void RefreshDllAndIcon(bool loadAll=true)
        {
            string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.dll");
            lstDlls.Items.Clear();
            foreach (var file in files)
            {
                try
                {
                    if (ReflectAble.GetTypes(file,loadAll).Count > 0)
                    {
                        lstDlls.Items.Add(Path.GetFileName(file));
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }

            string[] files1 = Directory.GetFiles(ToolMgr.Ins.ToolDirPath, "*.png");

            lstIcons.Items.Clear();
            foreach (string file in files1)
            {
                lstIcons.AddImage((Bitmap) Image.FromFile(file), Path.GetFileName(file));
            }
        }

        private void lstDlls_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDlls.SelectedItem == null)
            {
                return;
            }
            string fileName = Environment.CurrentDirectory + "\\" + lstDlls.SelectedItem;
            TypeList = ReflectAble.GetTypes(fileName);
            BindingSource bs = new BindingSource();
            bs.DataSource = TypeList;
            lstTypes.DataSource = bs;
            lstTypes.DisplayMember = "Name";
        }

        private void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            FunList = ReflectAble.GetMethods(lstTypes.SelectedItem as Type);
            BindingSource bs = new BindingSource();
            bs.DataSource = FunList;
            lstMethods.DataSource = bs;
            lstMethods.DisplayMember = "signature";
        }

        private void lstMethods_SelectedIndexChanged(object sender, EventArgs e)
        {
            ParameterInfo[] methodParams = (lstMethods.SelectedItem as MethodInfo)?.GetParameters();
            ParaTypes.Clear();
            ParaNames.Clear();
            if (methodParams != null)
                foreach (var parameter in methodParams)
                {
                    ParaTypes.Add(parameter.ParameterType.FullName);
                    ParaNames.Add(parameter.Name);
                }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshDllAndIcon(ckbLoadAll.Checked);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (var tool in ToolMgr.Ins.Tools)
            {
                if (tool.ItemName == txtNewToolName.Text)
                {
                    this.ShowErrorDialog($"工具名{txtNewToolName.Text}已存在，请重新输入");
                    return;
                }
            }
            Type tp = lstTypes.SelectedItem as Type;
            if (typeof(ActionItem).IsAssignableFrom(tp))
            {
                if (tp?.FullName != typeof(ActionItem).FullName)
                {
                    _customTool = (ActionItem)Activator.CreateInstance(tp);
                }
            }

            _customTool.ItemName = txtNewToolName.Text;
            _customTool.Dll = lstDlls.SelectedItem.ToString();
            _customTool.TypeName = (lstTypes.SelectedItem as Type)?.FullName;

            _customTool.MethodName = (lstMethods.SelectedItem as MethodInfo)?.Name;
            _customTool.IsStatic = ((MethodInfo)lstMethods.SelectedItem).IsStatic;
            _customTool.Icon = lstIcons.SelectedItem.Description;
            _customTool.Group = cbGroup.Text;
            _customTool.ParaTypes.Clear();
            _customTool.ParaNames.Clear();
            _customTool.ReturnType = (lstMethods.SelectedItem as MethodInfo)?.ReturnType.FullName;
            ParaTypes.ForEach(item => _customTool.ParaTypes.Add(item));
            ParaNames.ForEach(item => _customTool.ParaNames.Add(item));
            ToolMgr.Ins.SaveTool(_customTool);
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
